package org.stream.answerduel.controller.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.stream.answerduel.controller.BaseController;
import org.stream.answerduel.dto.PageResult;
import org.stream.answerduel.dto.Result;
import org.stream.answerduel.service.FriendService;
import org.stream.answerduel.service.UserService;
import org.stream.answerduel.service.WechatService;
import org.stream.answerduel.vo.FriendVo;
import org.stream.answerduel.vo.UserVo;

import java.util.List;

/**
 * 功能描述：用于处理对用户进行操作的请求与返回数据的控制器
 * @author  郭榕
 * @date   2019/7/10 13:16
 *
 */
@Api("用户控制器接口")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/user")
public class UserController extends BaseController
{
    @Autowired
    private UserService userService;

    @Autowired
    private FriendService friendService;

    @Autowired
    private WechatService wechatService;
    /**
     * @author 郭榕
     * @date   2019/7/10 17:17
     * @param  wechatId,nickName,avatarUrl,sessionKey
     * 功能描述：用于一开始登录的主界面的请求，返回用户头像旁的用户段位分数
     */
    @ApiOperation(value = "登录", notes = "返回用户信息")
    @ApiImplicitParams({
    })
    @GetMapping("/login")
    public Result<UserVo> userLogin(String wechatId , String nickname , String avatarUrl ,String sessionKey) throws Exception {
        UserVo userVo = userService.checkUserExist(wechatId,nickname,avatarUrl,sessionKey);
        System.out.println("用户登录......");
        return new Result<UserVo>().ok("用户信息如下",userVo);

    }

    /**
     * @author 郭榕
     * @date   2019/7/10 17:17
     * @param  code
     * 功能描述：获取code
     */
    @ApiOperation(value = "获取code", notes = "获取code解析并取到seesionKey")
    @ApiImplicitParams({
    })
    @GetMapping("/code")
    public Result<JSONObject> getCode(String code) throws Exception {

        String result = wechatService.getSessionKeyAndOppenId(code);

        JSONObject jsonObject = JSON.parseObject(result);
        System.out.println(jsonObject.toString());
        String sessionKey = jsonObject.get("sessionKey").toString();
        String openId     = jsonObject.get("openId").toString();
        log.info(sessionKey+"..." +openId);
        return new Result<>().ok("获取成功" , jsonObject);
    }

    /**
     * @author 郭榕
     * @date   2019/7/11 16:17
     * @param  wechatId
     * 功能描述：查询用户信息
     */
    @ApiOperation(value = "查询", notes = "返回需要显示的用户信息")
    @ApiImplicitParams({
    })
    @GetMapping("/information")
    public Result<UserVo> userInformation(String wechatId)
    {
        UserVo userVo = userService.findOneUserByWechatId(wechatId);
        return new Result<UserVo>().ok("用户已登录，信息如下",userVo);
    }

    /**
     * @author 郭榕
     * @date   2019/7/13 9:39
     * @param  userId
     * 功能描述：查询用户信息
     */
    @ApiOperation(value = "查询(入参是userid)", notes = "返回用户信息")
    @ApiImplicitParams({
    })
    @GetMapping("/hisInformation")
    public Result<UserVo> hisInformation(Long userId)
    {
        UserVo userVo = userService.findOneUserByUserid(userId);
        if(userVo == null)
        {
            return new Result<UserVo>().serverError("用户不存在",userVo);
        }
        return new Result<UserVo>().ok("用户信息如下",userVo);
    }

    /**
     * @author 郭榕
     * @date   2019/7/12 11:36
     * @param  wechatId
     * 功能描述：查询好友列表
     */
    @ApiOperation(value = "好友", notes = "返回需要显示的好友列表")
    @ApiImplicitParams({
    })
    @GetMapping("/friendlist")
    public Result<List<FriendVo>> userFriends(String wechatId)
    {
        List<FriendVo> friendVoList = userService.findFriendsByWechatid(wechatId);
        return new Result<List<FriendVo>>().ok("好友列表如下",friendVoList);
    }

    /**
     * 功能描述：新增用户
     * @param userVo
     * @return
     */
    @ApiOperation(value = "新增", notes = "新增数据")
    @ApiImplicitParams({
    })
    @PostMapping("/add")
    public ResponseEntity<Result> add(@RequestBody UserVo userVo) {
        userService.add(userVo);
        return new ResponseEntity(new Result().ok("添加用户成功"), HttpStatus.OK);
    }

    /**
     * 功能描述：删除用户信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除", notes = "删除数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户标识", required = true, dataType = "Long", example = "xxx"),
    })
    @DeleteMapping("/delete")
    public ResponseEntity<Result> del(Long id) {
        userService.del(id);
        return new ResponseEntity(new Result().ok("删除用户成功"), HttpStatus.OK);
    }

    /**
     * 功能描述：查询用户信息
     *
     * @return
     */
    @ApiOperation(value = "查询", notes = "查询数据")
    @ApiImplicitParams({
    })
    @PostMapping("/select")
    public ResponseEntity<PageResult<UserVo>> select(Integer pageSize, Integer pageNumber) {
        return new ResponseEntity(new Result().ok("查询用户信息成功", userService.select(pageSize, pageNumber)), HttpStatus.OK);
    }

    /**
     * 功能描述：修改用户信息
     *
     * @param userVo
     * @return
     */
    @ApiOperation(value = "修改", notes = "修改数据")
    @ApiImplicitParams({
    })
    @PutMapping("/update")
    public ResponseEntity<Result> update(@RequestBody UserVo userVo) {
        userService.update(userVo);
        return new ResponseEntity(new Result().ok("修改用户信息成功"), HttpStatus.OK);
    }

    /**
     * @author 郭榕
     * 功能描述:添加好友
     * @date 2019/7/17 16:44
     *
     */
    @ApiOperation(value = "加好友", notes = "添加好友")
    @ApiImplicitParams({})
    @GetMapping("/addFriend")
    public Result<String> addFriend( Long userId , Long friendId){
        String str = friendService.saveFriend(userId,friendId);
        return new Result<String>().ok("添加成功",str);
    }

    /**
     * @author 郭榕
     * 功能描述:删除好友
     * @date 2019/7/17 16:51
     *
     */
    @ApiOperation(value = "删好友", notes = "删除好友")
    @ApiImplicitParams({})
    @GetMapping("/deleteFriend")
    public Result<String> deleteFriend(Long userId , Long friendId){

        friendService.deleteFriend(userId,friendId);
        return new Result<String>().ok("删除成功","好友已删除");
    }

    /**
     * @author 郭榕
     * 功能描述：返回token已过期信息和状态码;
     * @date 2019/7/20 16:25
     */
    @ApiOperation(value = "过期", notes = "token过期信息")
    @ApiImplicitParams({})
    @ResponseBody
    @GetMapping("/tokenOverdue")
    public Result<String> tokenOverdue()
    {
        return new Result().tokenOverdue("token已过期或token未获取，请重新登录");
    }
}
